﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;

namespace WebUI.Controls
{
    public partial class TransferDetailsCtrl : BaseListControl, IListControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
        }

        public int DumpID
        {
            get
            {
                if (ViewState[ClientID + "_DumpID"] == null || !(ViewState[ClientID + "_DumpID"] is int))
                    ViewState[ClientID + "_DumpID"] = (int)0;
                return (int)ViewState[ClientID + "_DumpID"];
            }
            set { ViewState[ClientID + "_DumpID"] = value; }
        }


        public View_TransferDetails TransferDetails
        {
            get { return (View_TransferDetails)SourceObject; }
            set { SourceObject = value; }
        }

        #region IListControl Members

        public void DeleteCheckedItems()
        {
            String checkedItemID = "";
            for (int i = 0; i < grid.Items.Count; i++)
            {
                DataGridItem item = grid.Items[i];
                Control ctrl = item.FindControl("cbSelect");
                if (ctrl != null && ctrl is CheckBox && ((CheckBox)ctrl).Checked && ((CheckBox)ctrl).Attributes["ItemID"] != null)
                    checkedItemID += ((CheckBox)ctrl).Attributes["ItemID"].ToString() + ",";
            }
            if (checkedItemID != "")
                checkedItemID = checkedItemID.Remove(checkedItemID.Length - 1);
            checkedItemID = "(" + checkedItemID + ")";

            String oldFilter = Filter;
            if (!String.IsNullOrEmpty(oldFilter))
                Filter = Filter + " and (" + View_TransferDetails.ColumnNames.ID + " in " + checkedItemID + ")";
            else
                Filter = View_TransferDetails.ColumnNames.ID + " in " + checkedItemID;

            TransferDetails.Rewind();
            while(TransferDetails.RowCount > 0)
            {
                TransferDetails.MarkAsDeleted();
                TransferDetails.MoveNext();
            }

            //if (!SaveOnCommit)
            //    SaleItems.Save();

            Filter = oldFilter;
        }

        public void DeleteSelectedItem()
        {
            throw new NotImplementedException();
        }

        public void AddNewItem(MyGeneration.dOOdads.SqlClientEntity newItem)
        {
            throw new NotImplementedException();
        }

        public void UpdateItem(MyGeneration.dOOdads.SqlClientEntity updatedItem)
        {
            throw new NotImplementedException();
        }

        #endregion

        public void AddIMEI(string imei)
        {
            View_ProductDetails pd = View_ProductDetails.GetAvailableProductDetailsByIMEI(imei);
            if (pd.RowCount <= 0 || pd.BranchID != Branches.CurrentBranchID)
                throw new ApplicationException(string.Format("Product {0} is not available in stock.", imei));

            string oldFilter = TransferDetails.Filter;
            TransferDetails.Filter = View_TransferDetails.ColumnNames.IMEI + "='" + imei + "'";
            if (TransferDetails.RowCount > 0)
            {
                TransferDetails.Filter = oldFilter;
                throw new ApplicationException(string.Format("Product {0} has already been added.", imei));
            }
            TransferDetails.Filter = oldFilter;
            TransferDetails.AddNew();
            try
            {
                TransferDetails.ID = --DumpID;
                TransferDetails.s_ProductID = pd.s_ProductID;
                TransferDetails.s_BrandName = pd.s_BrandName;
                TransferDetails.s_CategoryName = pd.s_CategoryName;
                TransferDetails.s_IMEI = imei;
                TransferDetails.s_ProductName = pd.s_ProductName;
                TransferDetails.Quantity = 1;
            }
            catch (Exception ex)
            {
                TransferDetails.MarkAsDeleted();
                throw ex;
            }
            Refresh();
        }

        public void AddNonIMEIProduct(int productID, int qty)
        {
            View_Products pd = View_Products.LoadByProductID(productID);
            if (pd.RowCount <= 0)
                throw new ApplicationException(string.Format("Product is not available in stock."));

            if (pd.Quantity < qty)
                throw new ApplicationException(string.Format("Not enough quantity to transfer."));

            string oldFilter = TransferDetails.Filter;
            TransferDetails.Filter = View_TransferDetails.ColumnNames.ProductID + "=" + productID.ToString();
            if (TransferDetails.RowCount > 0)
            {
                TransferDetails.Quantity += qty;
                if (TransferDetails.Quantity > pd.Quantity)
                {
                    TransferDetails.Quantity -= qty;
                    TransferDetails.Filter = oldFilter;
                    throw new ApplicationException(string.Format("Not enough quantity to transfer."));
                }
                TransferDetails.Filter = oldFilter;
            }
            else
            {
                TransferDetails.Filter = oldFilter;
                TransferDetails.AddNew();
                try
                {
                    TransferDetails.ID = --DumpID;
                    TransferDetails.s_ProductID = pd.s_ID;
                    TransferDetails.s_BrandName = pd.s_BrandName;
                    TransferDetails.s_CategoryName = pd.s_CategoryName;
                    TransferDetails.s_IMEI = "";
                    TransferDetails.s_ProductName = pd.s_ProductName;
                    TransferDetails.Quantity = qty;
                }
                catch (Exception ex)
                {
                    TransferDetails.MarkAsDeleted();
                    throw ex;
                }
            }
            Refresh();
        }
    }
}